
//这个版本的加载.proto文件 是旧版的 定义自动需要定义 required token
if (typeof dcodeIO === 'undefined' || !dcodeIO.ProtoBuf) {
    throw(new Error("ProtoBuf.js is not present. Please see www/index.html for manual setup instructions."));
}

// Initialize ProtoBuf.js
let ProtoBuf = dcodeIO.ProtoBuf;
let ProroFile = null;
//不设置callback是同步获取,主线程会阻塞;并且会有警告
ProroFile = ProtoBuf.loadProtoFile("proto/copyInfo.proto");  //直接加载文件
//console.log("ProtoBuf.loadProto",ProtoBuf.loadProto); //加载字符串


console.log("ProroFile:",ProroFile);
var CopyInfo = ProroFile.build("CopyInfo"); 
let cinf = new CopyInfo()
console.log("CopyInfo inst cinf 00",CopyInfo.decode);
console.log("CopyInfo inst cinf cinf.constructor.name:",cinf.constructor);

let cls = ProroFile.build("CopyReward");
console.log("cls",cls);

let crw = new cls();
crw.itemId = 1021003;
crw.title = "副本奖励";
crw.count = 4;
crw.desc = "丰富的奖励来自你个人的努力";
console.log("crw",crw);


/*
var ChildrenModule = ProtoBuf.loadProtoFile("proto/test.proto").build("ChildrenModule");
let cm = new ChildrenModule();
console.log("cm",cm);
*/

let ProtoUtils = {}

//构建一个protobuf类
ProtoUtils.build =function (msgName) {
    let msgCls = null;
    try {
        msgCls = ProroFile.build(msgName);
    } catch (error) {
        console.log("build error:",error);        
    }
    if (!msgCls) {
        throw new Error(`this no message name call ${msgName}`);
    }
    //let inst = new msgCls();
    return msgCls;
}

ProtoUtils.create = function (msgName){
    let cls = ProtoUtils.build(msgName);
    return new cls();
}


ProtoUtils.decode =function (msgName,byteData) {
    let msgCls = ProtoUtils.build(msgName);
    let msgObj = msgCls.decode(byteData); //decode是类属性,不是实例属性,之前一直用实例访问是错误的
    return msgObj;
}






